package com.simon.hse.activity.danger;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.adapter.CommonEmptyAdapter;
import com.simon.hse.adapter.CommonViewHolder;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.DangerAddLevelInput;
import com.simon.hse.http.entity.DangerAddLevelOutput;
import com.simon.hse.utils.ToastUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

/**
 * 隐患排查级别
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_danger_eliminate_level)
public class DangerEliminateLevelActivity extends BaseActivity {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @ViewInject(R.id.et_danger_eliminate_level_search)
    EditText mEt_danger_eliminate_level_search;

    @ViewInject(R.id.et_danger_eliminate_level_content)
    EditText mEt_danger_eliminate_level_content;

    @ViewInject(R.id.common_recyclerview)
    RecyclerView mRecyclerView;

    private int currentPage = 1, totalPage = 1;
    private CommonEmptyAdapter mCommonEmptyAdapter;
    private List<DangerAddLevelOutput.DataBean.PageDataBean> mDataBeanList = new ArrayList<>();
    private int choosePos = -1;// 选中项

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTv_common_title.setText("隐患上报");

        initRecyclerView();
        initData();
    }

    @Event(value = {R.id.iv_common_left, R.id.tv_danger_eliminate_level_confirm})
    private void clickButton(View view) {
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;
            case R.id.tv_danger_eliminate_level_confirm:
                DangerAddLevelOutput.DataBean.PageDataBean pageDataBean = mDataBeanList.get(choosePos);
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("DANGER_LEVEL", pageDataBean);
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                finishActivity(DangerEliminateLevelActivity.this);
                break;
        }
    }

    private void initRecyclerView() {
        mEt_danger_eliminate_level_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                initData();
            }
        });

        LinearLayoutManager llm = new LinearLayoutManager(DangerEliminateLevelActivity.this);
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        mCommonEmptyAdapter = new CommonEmptyAdapter(DangerEliminateLevelActivity.this, R.layout.layout_item_danger_eliminate_level_add, mDataBeanList);
        mCommonEmptyAdapter.setItemDatasListener(new CommonEmptyAdapter.ItemDatasListener<DangerAddLevelOutput.DataBean.PageDataBean>() {
            @Override
            public void setItemDatas(CommonViewHolder holder, DangerAddLevelOutput.DataBean.PageDataBean pageDataBean, int position) {
                TextView tv_standard_eliminate_level_content = holder.getView(R.id.tv_standard_eliminate_level_content);
                TextView tv_standard_eliminate_level_level = holder.getView(R.id.tv_standard_eliminate_level_level);
                ImageView iv_standard_eliminate_level_state = holder.getView(R.id.iv_standard_eliminate_level_state);

                tv_standard_eliminate_level_content.setText(pageDataBean.getInvestigationContents());
                tv_standard_eliminate_level_level.setText(pageDataBean.getRiskLevelText());
                if (choosePos == position) {
                    iv_standard_eliminate_level_state.setSelected(true);
                } else {
                    iv_standard_eliminate_level_state.setSelected(false);
                }
            }
        });

        mCommonEmptyAdapter.setOnItemClickListener(new CommonEmptyAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClickListener(View view, int position) {
                choosePos = position;
                mCommonEmptyAdapter.notifyDataSetChanged();
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mRecyclerView.setAdapter(mCommonEmptyAdapter);
    }

    private void initData() {
        mDataBeanList.clear();
        DangerAddLevelInput dangerAddLevelInput = new DangerAddLevelInput();
        DangerAddLevelInput.ModelBean modelBean = new DangerAddLevelInput.ModelBean();
        String content = mEt_danger_eliminate_level_search.getText().toString();
        modelBean.setInvestigationContents(content);

        dangerAddLevelInput.setModel(modelBean);
        dangerAddLevelInput.setLimit(20 * currentPage);
        dangerAddLevelInput.setOffset(20 * (currentPage - 1));

        Call<String> call = HttpManager.getInstance().requestService().DANGER_ADD_LEVEL(HttpManager.getInstance().requestBody(dangerAddLevelInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                DangerAddLevelOutput dangerEliminateOutput = new Gson().fromJson(result, DangerAddLevelOutput.class);
                if (dangerEliminateOutput.getResult().equals("Success")) {
                    totalPage = dangerEliminateOutput.getData().getRowCount();
                    List<DangerAddLevelOutput.DataBean.PageDataBean> dataBeanList = dangerEliminateOutput.getData().getPageData();
                    mDataBeanList.addAll(dataBeanList);
                    mCommonEmptyAdapter.notifyDataSetChanged();
                } else {
                    ToastUtils.getInstance().showShortToastBottom(dangerEliminateOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }
}
